package com.user.basic.analyze.age;

import com.entity.analyze.Age;
import com.entity.dim.Dim;
import com.user.basic.analyze.BaseAnalyzeService;
import com.user.basic.mapper.DimMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class AgeAnalyzeServiceImpl extends BaseAnalyzeService<AgeAnalyzeEntity> implements AgeAnalyzeService {

    private final AgeAnalyzeMapper ageAnalyzeMapper;

    @Resource
    private DimMapper dimMapper;

    public AgeAnalyzeServiceImpl(AgeAnalyzeMapper ageAnalyzeMapper) {
        this.ageAnalyzeMapper = ageAnalyzeMapper;
    }

    @Override
    public void group() throws IllegalAccessException {
        //统计15岁~65岁总人数
        Calendar instance = Calendar.getInstance();
        Calendar clone = (Calendar) instance.clone();
        clone.add(Calendar.YEAR, -65);
        clone.set(clone.get(Calendar.YEAR), 0, 1, 0, 0, 0);
        instance.add(Calendar.YEAR, -15);
        instance.set(instance.get(Calendar.YEAR), 11, 31, 23, 59, 59);
        int count = ageAnalyzeMapper.count(clone.getTime(), instance.getTime());
        if (count < 500000) {
            List<Date> dates = ageAnalyzeMapper.selectByBirthday(clone.getTime(), instance.getTime());

            List<Dim> dimList = null;
            if (dates != null && dates.size() > 0) {
                dimList = dimMapper.selectByTypeAndStatus(1, (byte) 1);
            }
            Map<String, Age> ageHashMap = new HashMap<>();
            if (dimList != null) {
                List<Dim> finalDimList = dimList;
                AtomicReference<String> key = new AtomicReference<>();
                dates.forEach(date -> {
                    finalDimList.forEach(age -> {
                        String[] ages = age.getValue().split("-");
                        if ((1900 + date.getYear()) >= Integer.valueOf(ages[0])
                                && (1900 + date.getYear()) <= Integer.valueOf(ages[1])) {
                            key.set(age.getName());
                        }
                    });
                    ageHashMap.compute(key.get(), (t, u) -> {
                        if (u == null) {
                            Age age = new Age();
                            age.setTotal(1);
                            return age;
                        } else {
                            u.setTotal(u.getTotal() + 1);
                            return u;
                        }
                    });
                });
                save(ageHashMap, count);
            }
        } else {
            throw new IllegalAccessException("请切换分析组件");
        }
    }

    private void save(Map<String, Age> ageHashMap, int count) {
        ageHashMap.forEach((s, age) -> {
            age.setName(s);
            age.setPercent(Float.parseFloat(String.format("%.2f",
                    (((float) age.getTotal()) / ((float) count) * 100))));
            age.setId(UUID.randomUUID().toString());
            age.setCreateTime(new Date());
            ageAnalyzeMapper.save(age);
        });
    }


}
